package com.gitee.swsk33.jdbcdemo;

import com.gitee.swsk33.jdbcdemo.config.JDBCConfig;
import com.gitee.swsk33.jdbcdemo.dao.CharacterDAO;
import com.gitee.swsk33.jdbcdemo.dao.impl.CharacterDAOImpl;
import com.gitee.swsk33.jdbcdemo.dataobject.Character;

import java.sql.ResultSet;

public class Main {

	// 实例化查询层
	private static CharacterDAO characterDAO = new CharacterDAOImpl();

	public static void main(String[] args) throws Exception {
		// 开始时连接数据库
		JDBCConfig.connect();
		// 新建一个角色
		Character addCharacter = new Character();
		addCharacter.setName("宫子");
		addCharacter.setNickname("布丁");
		addCharacter.setType("前卫物理坦克");
		addCharacter.setGuild("恶魔伪王国军");
		System.out.println("插入条数：" + characterDAO.insert(addCharacter));
		// 查询刚插入的角色试试
		Character character = characterDAO.getById(4);
		System.out.println("刚刚插入的角色：" + character.getId() + " " + character.getName() + " " + character.getNickname() + " " + character.getType() + " " + character.getGuild());
		// 删除刚加的角色试试
		System.out.println("删除条数：" + characterDAO.delete(character.getId()));
		// 修改一个角色
		// 先取出一个角色（取出角色 美美）
		Character getCharacter = characterDAO.getById(2);
		// 修改美美的外号为 兔帽子卡通女孩 并更新至数据库
		getCharacter.setNickname("兔帽子卡通女孩");
		System.out.println("更新条数：" + characterDAO.update(getCharacter));
		// 查询刚插入的角色试试
		Character getUpdateCharacter = characterDAO.getById(2);
		System.out.println("刚刚修改的角色：" + getUpdateCharacter.getId() + " " + getUpdateCharacter.getName() + " " + getUpdateCharacter.getNickname() + " " + getUpdateCharacter.getType() + " " + getUpdateCharacter.getGuild());
		// 查询所有角色
		ResultSet all = characterDAO.getAll();
		System.out.println("当前全部角色：");
		while (all.next()) {
			System.out.println(all.getInt("id") + " " + all.getString("name") + " " + all.getString("nickname") + " " + all.getString("type") + " " + all.getString("guild"));
		}
		// 最后记得关闭连接
		JDBCConfig.disConnect();
	}

}